<?php

/*
 * Test whether a number is palindrome number: abcba
 * A number is palindrome if:
 * 1) LSB and MSB digit are the same;
 * 2) The number formed by removing the LSB and MSB is palindrome
 * */

//$number = rand(1, 999999);
$number = 239919932;

echo "$number<br/>";

$result = testPalindrome($number);

echo $result ? "Is palindrome" : "Is not palindrome";

function testPalindrome( $number ){
	// Should be positive number
	if( $number < 0 ){
		return false;
	}

	if( $number < 10 ){
		// Only 1 digit
		return true;
	}

	// Push the digits into a array
	$digits = array();
	while( $number > 0 ){
		$digits[] = $number % 10;
		$number = floor( $number / 10 );
	}

	// number abcba becomes array {a, b, c, b, a}
	var_dump( $digits );

	return testPalindromeArrayRecursive( $digits );
}

function testPalindromeArrayRecursive( $array ){
	$length = count( $array );
	if( 1 == $length ){
		return true;
	}

	if( 2 == $length ){
		if( $array[0] == $array[1] ){
			return true;
		}
		else {
			return false;
		}
	}

	if( $array[0] == $array[$length-1] ){
		return testPalindromeArrayRecursive( array_slice($array, 1, $length - 2) );
	}
	else {
		return false;
	}
}